/**
 * yeahmobi.com Inc.
 * Copyright © 2014 YeahMobi | All Rights Reserved.
 */

package com.ndp.fb.mq.service.schedulerservice;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.mq.api.service.schedulerservice.AdAccountsSchedulerService;
import com.ndp.fb.rdb.api.EntrustAccountRdbService;
import com.ndp.fb.rdb.api.FbUserPermissionRdbService;
import com.ndp.fb.rdb.api.FbUserRdbService;
import com.ndp.fb.rdb.model.AdAccountPermission;
import com.ndp.fb.rdb.model.EntrustAccount;
import com.ndp.fb.redis.api.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author giser.yang
 * @Title: AdaccountsSchedulerServiceImpl.java
 * @Copyright: Copyright © 2015
 * @Description:
 * @Company: yeahmobi
 * @Created on 下午4:29:09
 */
@Service
public class AdAccountsSchedulerServiceImpl implements AdAccountsSchedulerService {
    private static Logger logger = LoggerFactory.getLogger(AdAccountsSchedulerServiceImpl.class);
    @Reference
    FbUserPermissionRdbService fbUserPermissionRdbService;
    @Reference
    EntrustAccountRdbService entrustAccountRdbService;
    @Reference
    FbUserRdbService fbUserRdbService;
    @Autowired
    private RedisClient redisClient;

    /**
     * @return
     * @discription 如果有admin权限的token优先返回admin权限的token
     * @author giser.yang
     * 创建时间 2015年6月2日 下午4:29:09
     * @see AdAccountsSchedulerService ()
     */

    @Override
    public String getAdaccountsToken(Long accountId) {
        List<AdAccountPermission> userPermissions = fbUserPermissionRdbService.getUserpermissions(accountId);
        if (CollectionUtils.isEmpty(userPermissions)) {
//          entrustAccountRdbService.deleteAdAccount(String.valueOf(accountId));
            logger.error("账号:" + accountId + "没有用户使用它");
            return null;
        }
        Map<String, Long> roleUserId = new HashMap<String, Long>();
        for (AdAccountPermission userPermission : userPermissions) {
            if(userPermission.getStatus().equalsIgnoreCase("ACTIVE")){
                roleUserId.put(userPermission.getRole(), userPermission.getFbUserId());
            }
        }
        if (roleUserId.containsKey("ADMIN")) {
            return getToken(roleUserId.get("ADMIN"));
        }
        if (roleUserId.containsKey("GENERAL_USER")) {
            return getToken(roleUserId.get("GENERAL_USER"));
        }
        if (roleUserId.containsKey("REPORTS_ONLY")) {
            return getToken(roleUserId.get("REPORTS_ONLY"));
        }
        return null;
    }

    private String getToken(Long fbUserId) {
        return fbUserRdbService.getTokenByFbUserId(fbUserId);
    }

    /**
     * @param roles
     * @return
     * @discription
     * @author giser.yang
     * 创建时间 2015年6月9日 上午11:32:00
     */
    @Override
    public List<EntrustAccount> getAdAccountByRoles(String... roles) {
        return entrustAccountRdbService.findAdAccountListByRoles(roles);
    }

    /**
     * @param accountId
     * @return
     * @discription
     * @author giser.yang
     * 创建时间 2015年7月22日 下午12:21:31
     * @see AdAccountsSchedulerService#getAdaccountByEntrustAccountId(java.lang.Long)
     */
    @Override
    public EntrustAccount getAdaccountByEntrustAccountId(Long accountId) {
        return entrustAccountRdbService.findByFbAccountId(accountId);
    }
}
